package com.ph.finder.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;

public class ClearCityCompletionImpl extends HttpServlet {

	private static final long serialVersionUID = 5778222784927847336L;

	// Not Compatible with Split Sending
	private static final String okaeri = "okaeri=CHANGEME";

	@SuppressWarnings("unchecked")
	@Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println(req.getQueryString());
		if (!req.getQueryString().contains(okaeri)) {
			resp.setContentType("text/plain");
			resp.setStatus(400);
			return;
		}

    // Work on the entire list of points on the DS
		Query qAll = new Query("CityCmplte").setKeysOnly();
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		List<Entity> list = ds.prepare(qAll).asList(FetchOptions.Builder.withLimit(500));
		List<Key> keys = new ArrayList<Key>(list.size());
		for (Entity entity : list) {
			keys.add(entity.getKey());
		}
		ds.delete(keys);

		resp.setContentType("text/plain");
		resp.setStatus(200);
	}
}
